<?php

namespace app\modules\Cruise\models;

use Yii;

/**
 * This is the model class for table "image".
 *
 * @property integer $id
 * @property string $product_id
 * @property string $name
 * @property integer $type
 * @property integer $cover
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class Image extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'image';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['product_id'], 'required'],
            [['type', 'cover', 'status', 'user_id'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['product_id'], 'string', 'max' => 11],
            [['name'], 'string', 'max' => 128],
        ];
    }


    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
            
            // 保存后将图片上传置七牛
            $file_path = sprintf("%s/web/%s",yii::$app->basePath, $this->path);;
            $file_name = $this->path;
            $re = yii::$app->qiniu->uploadQiniu($file_path, $file_name);
        }
        return parent::beforeSave($insert);
    }//}}}


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app', 'Product ID'),
            'name' => Yii::t('app', 'Name'),
            'type' => Yii::t('app', 'Type'),
            'cover' => Yii::t('app', 'Cover'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
